package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.ClueDto;
import com.qk.entity.Clue;
import com.qk.service.ClueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("clues")
public class ClueController {
    @Autowired
    private ClueService clueService;

    @PostMapping
    public Result add(@RequestBody Clue clue) {
        log.info("add {}", clue);
        clue.setStatus(1);
        clue.setCreateTime(LocalDateTime.now());
        clue.setUpdateTime(LocalDateTime.now());
        clueService.save(clue);
        return Result.success();
    }

    @GetMapping
    public Result listClue(ClueDto clueDto) {
        PageResult<Clue> pageResult = clueService.listClue(clueDto);
        return Result.success(pageResult);
    }

    @PutMapping("/assign/{clueId}/{userId}")
    public Result assign(@PathVariable Integer clueId, @PathVariable Integer userId) {
        Clue clue = new Clue();//创建一个clue对象
        clue.setId(clueId);//设置id
        clue.setUserId(userId);//设置userId
        clue.setUpdateTime(LocalDateTime.now());//设置更新时间
        clue.setStatus(2);//设置状态
        clueService.updateById(clue);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Clue clue = clueService.getClueById(id);
        return Result.success(clue);
    }

    @PutMapping
    public Result update(@RequestBody Clue clue) {
        log.info("update {}", clue);
        clueService.upClue(clue);
        return Result.success();
    }

    @PutMapping("/false/{id}")
    public Result falseClue(@PathVariable Integer id, @RequestBody Clue clue) {
        log.info("falseClue {}", clue);
        clueService.updateByIdb(id, clue);
        return Result.success();
    }

    @PutMapping("/toBusiness/{id}")
    public Result toBusiness(@PathVariable Integer id) {
        log.info("toBusiness {}", id);
        clueService.toBusiness(id);
        return Result.success();
    }

    @GetMapping("/pool")
    public Result listPool(ClueDto clueDto) {
        PageResult<Clue> pageResult = clueService.listPool(clueDto);
        return Result.success(pageResult);
    }
}
